package com.acegear.horizon.domain.repository.jpa;

import com.acegear.horizon.domain.models.jpa.PostComment;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;
import java.util.Optional;

/**
 * Created by wangsike on 2017/2/6.
 */
public interface PostCommentRepository extends PagingAndSortingRepository<PostComment, Long> {
    Optional<PostComment> findTopByPostIdOrderByIdDesc(Long postId);

    List<PostComment> findByPostIdAndDeletedOrderByIdAsc(Long postId, boolean deleted, Pageable pageable);

    List<PostComment> findByUserIdAndDeletedOrderByIdAsc(Long userId, boolean deleted, Pageable pageable);

    List<PostComment> findByUserIdAndPostIdOrderByIdAsc(Long userId, Long postId);

    @Query("select c.postId from PostComment as c where c.userId = ?1 and c.deleted = false group by c.postId order by c.postId desc")
    List<Long> findCommentedPostByUserId(Long userId, Pageable pageable);

    Integer countByPostIdAndDeleted(Long postId, boolean deleted);

    Integer countByUserIdAndDeleted(Long userId, boolean deleted);

    List<PostComment> findAll();

    PostComment findByCommentId(Long commentId);
}
